package com.ruoyi.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.bo.PAnswerLogBo;
import com.ruoyi.system.domain.vo.PAnswerLogVo;
import com.ruoyi.system.service.IPAnswerLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 测试答案记录Controller
 *
 * @author ruoyi
 * @date 2022-08-24
 */
@Validated
@Api(value = "测试答案记录控制器", tags = {"测试答案记录管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/answerLog")
public class PAnswerLogController extends BaseController {

    private final IPAnswerLogService iPAnswerLogService;

    /**
     * 查询测试答案记录列表
     */
    @ApiOperation("查询测试答案记录列表")
    @SaCheckPermission("system:answerLog:list")
    @GetMapping("/list")
    public TableDataInfo<PAnswerLogVo> list(PAnswerLogBo bo, PageQuery pageQuery) {
        return iPAnswerLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出测试答案记录列表
     */
    @ApiOperation("导出测试答案记录列表")
    @SaCheckPermission("system:answerLog:export")
    @Log(title = "测试答案记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PAnswerLogBo bo, HttpServletResponse response) {
        List<PAnswerLogVo> list = iPAnswerLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "测试答案记录", PAnswerLogVo.class, response);
    }

    /**
     * 获取测试答案记录详细信息
     */
    @ApiOperation("获取测试答案记录详细信息")
    @SaCheckPermission("system:answerLog:query")
    @GetMapping("/{id}")
    public R<PAnswerLogVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(iPAnswerLogService.queryById(id));
    }

    /**
     * 新增测试答案记录
     */
    @ApiOperation("新增测试答案记录")
    @SaCheckPermission("system:answerLog:add")
    @Log(title = "测试答案记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PAnswerLogBo bo) {
        return toAjax(iPAnswerLogService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改测试答案记录
     */
    @ApiOperation("修改测试答案记录")
    @SaCheckPermission("system:answerLog:edit")
    @Log(title = "测试答案记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PAnswerLogBo bo) {
        return toAjax(iPAnswerLogService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除测试答案记录
     */
    @ApiOperation("删除测试答案记录")
    @SaCheckPermission("system:answerLog:remove")
    @Log(title = "测试答案记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iPAnswerLogService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
